/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.BusinessLogicLayer;

import edu.nii.qlks.DataAccessLayer.MSSQLConnectUnit;
import edu.nii.qlks.DataTransferObject.PhieuThuInfo;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author MARS_NHO
 */
public class PhieuThuBLL {
    MSSQLConnectUnit _connect;
    ResultSet  _result;
 
    
    public ArrayList LayDS(String Condition, String OderBy) throws Exception{
        ArrayList vt = new ArrayList();
        
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _result = _connect.Select("PHIEUTHU", Condition, OderBy);
        
        while (_result.next()){
            ArrayList vt_row = new ArrayList();
            vt_row.add(_result.getString(1));
            vt_row.add(_result.getString(2));
            vt_row.add(_result.getString(3));
            vt_row.add(_result.getString(4));
            vt_row.add(_result.getString(5));
            vt_row.add(_result.getString(6));
            vt_row.add(_result.getString(7));
            vt.add(vt_row);
        }
        _connect.Close();
        return vt;
    }
    
    public ArrayList LayDS(String Condition) throws Exception{
         return LayDS(Condition, null);
    }
    
    public ArrayList LayDS() throws Exception{
        return LayDS(null);
    }
    
    public void Insert(PhieuThuInfo phieuThuInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        HashMap<String, Object> map = new HashMap<>();
        map.put("MaPhieuThu", phieuThuInfo.getMaPhieuThu());
        map.put("MaKhachHang", phieuThuInfo.getMaKhachHang());
        map.put("NgayThanhToan", phieuThuInfo.getNgayThanhToan());
        map.put("HuHao", phieuThuInfo.getHuHao());
        map.put("TriGiaBangKe", phieuThuInfo.getTriGiaBangKe());
        map.put("TriGiaHoaDon", phieuThuInfo.getTriGiaHoaDon());
        map.put("TongTriGia", phieuThuInfo.getTongTriGia());
        _connect.Insert("PHIEUTHU", map);
    }
    
     
    public void Inserts(HashSet<PhieuThuInfo> phieuThuInfos) throws Exception{
        for (PhieuThuInfo phieuThuInfo:phieuThuInfos) {
            this.Insert(phieuThuInfo);
        }
    }
    
    public void Update(PhieuThuInfo phieuThuInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        HashMap<String, Object> map = new HashMap<>();
        map.put("MaKhachHang", phieuThuInfo.getMaKhachHang());
        map.put("NgayThanhToan", phieuThuInfo.getNgayThanhToan());
        map.put("HuHao", phieuThuInfo.getHuHao());
        map.put("TriGiaBangKe", phieuThuInfo.getTriGiaBangKe());
        map.put("TriGiaHoaDon", phieuThuInfo.getTriGiaHoaDon());
        map.put("TongTriGia", phieuThuInfo.getTongTriGia());
        _connect.Update("PHIEUTHU", map, "MaPhieuThu = '" + phieuThuInfo.getMaPhieuThu() + "'");
    }
    
    public void Updates(HashSet<PhieuThuInfo> phieuThuInfos) throws Exception{
        for (PhieuThuInfo phieuThuInfo:phieuThuInfos){
            this.Update(phieuThuInfo);
        }
    }
    
    public void Delete(PhieuThuInfo phieuThuInfo) throws Exception{
        _connect = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.getConnection();
        _connect.Delete("PHIEUTHU", "MaPhieuThu = '" + phieuThuInfo.getMaPhieuThu() + "'");
    }
    
    public void Deletes(HashSet<PhieuThuInfo> phieuThuInfos) throws Exception{
         for (PhieuThuInfo phieuThuInfo:phieuThuInfos){
            this.Delete(phieuThuInfo);
        }
    }
    
    public String TaoMaTuDong(String maGoc, int batDau){
        String ma = edu.nii.qlks.BusinessLogicLayer.BusinessLogicAbstract.TaoMaTuDong(maGoc, batDau);
        return ma;
    }
}
